package jsystem.certman;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.security.Key;
import java.security.KeyStore;
import java.security.Provider.Service;
import java.security.UnrecoverableKeyException;
import java.security.cert.Certificate;
import java.security.cert.CertificateFactory;
import java.security.cert.X509Certificate;
import java.util.Collection;
import java.util.Enumeration;
import java.util.Iterator;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.DefaultListModel;
import javax.swing.Icon;
import javax.swing.JComponent;
import javax.swing.JFileChooser;
import javax.swing.JList;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.table.DefaultTableModel;
import jbrowser.gui.Page;

public class CertificateManager extends JPanel implements Page, ActionListener {

    private static final Logger logger = Logger.getLogger(CertificateManager.class.getName());
    private static final char[] PASSWORD = "jbrowser".toCharArray();
    private File keyStoreDir;
    private String keyStoreName;
    private final JFileChooser fileChooser = new JFileChooser();

    @Override
    public void actionPerformed(ActionEvent e) {
        if ("NEW_KEYSTORE".equals(e.getActionCommand())) {
            String fileName = JOptionPane.showInputDialog(null, "New keystore file name: ", "New", JOptionPane.QUESTION_MESSAGE);
            if (fileName != null && !fileName.isEmpty()) {
                File file = new File(keyStoreDir, fileName);
                if (!file.exists()) {
                    try {
                        if (file.createNewFile()) {
                            DefaultListModel listModel = (DefaultListModel) lstkeyStore.getModel();
                            listModel.addElement(file.getName());
                        }
                    } catch (IOException ex) {
                        logger.log(Level.SEVERE, null, ex);
                    }
                }
            }
        } else if ("DELETE_KEYSTORE".equals(e.getActionCommand())) {
            String fileName = lstkeyStore.getSelectedValue().toString();
            int i = JOptionPane.showConfirmDialog(null, "Are you sure you want delete this keystore: " + fileName, "Delete keystore", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE);
            if (i == JOptionPane.YES_OPTION) {
                File file = new File(keyStoreDir, fileName);
                if (file.delete()) {
                    DefaultListModel listModel = (DefaultListModel) lstkeyStore.getModel();
                    listModel.removeElement(lstkeyStore.getSelectedValue());
                }
            }
        } else if ("IMPORT_KEYSTORE".equals(e.getActionCommand())) {
        } else if ("OPEN_KEYSTORE".equals(e.getActionCommand())) {
            if (fileChooser.showOpenDialog(this) == JFileChooser.APPROVE_OPTION) {
                showKeyStoreProperties(fileChooser.getSelectedFile());
            }
        } else if ("DELETE_KEYSTORE".equals(e.getActionCommand())) {
            String fileName = lstkeyStore.getSelectedValue().toString();
            int i = JOptionPane.showConfirmDialog(null, "Are you sure you want delete this keystore: " + fileName, "Delete keystore", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE);
            if (i == JOptionPane.YES_OPTION) {
                File file = new File(keyStoreDir, fileName);
                if (file.delete()) {
                    DefaultListModel listModel = (DefaultListModel) lstkeyStore.getModel();
                    listModel.removeElement(lstkeyStore.getSelectedValue());
                }
            }
        } else if ("ADD_CERT_FILE".equals(e.getActionCommand())) {
            if (fileChooser.showOpenDialog(this) == JFileChooser.APPROVE_OPTION) {
                addCertificateToKeyStore(new File(keyStoreDir, keyStoreName), fileChooser.getSelectedFile());
            }
        } else if ("ADD_CERT_URL".equals(e.getActionCommand())) {
            String hostName = JOptionPane.showInputDialog(null, "Enter host name: ", "Add certificate", JOptionPane.QUESTION_MESSAGE);
            if (hostName != null && !hostName.isEmpty()) {
                File file = new File(keyStoreDir, keyStoreName);
                try {
                    CertificateInstaller.install(file, hostName, PASSWORD);
                } catch (Exception ex) {
                    JOptionPane.showMessageDialog(null, ex.getMessage(), "Error", JOptionPane.ERROR_MESSAGE);
                    logger.log(Level.SEVERE, null, ex);
                }
            }
        } else if ("DELETE_CERT".equals(e.getActionCommand())) {
        }
    }

    public CertificateManager() throws Exception {
        initComponents();

        keyStoreDir = new File(System.getProperty("jbrowser.home"), "security");
        if (!keyStoreDir.exists()) {
            keyStoreDir.mkdir();
        }

        btnNewKeyStore.addActionListener(this);
        btnDeleteKeyStore.addActionListener(this);
        btnAddCertURL.addActionListener(this);
        btnAddCertFile.addActionListener(this);
        btnRemoveCertificate.addActionListener(this);
        btnImportKeyStore.addActionListener(this);
        btnOpenKeyStore.addActionListener(this);

        DefaultListModel listModel = new DefaultListModel();
        lstkeyStore.setModel(listModel);
        for (File certFile : keyStoreDir.listFiles()) {
            listModel.addElement(certFile.getName());
        }
        lstkeyStore.setModel(listModel);

        lstkeyStore.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent evt) {
                JList list = (JList) evt.getSource();
                if (evt.getClickCount() == 2) {
                    keyStoreName = list.getSelectedValue().toString();
                    showKeyStoreProperties(new File(keyStoreDir, keyStoreName));
                }
            }
        });
    }

    private void addCertificateToKeyStore(File keyStoreFile, File certFile) {
        char[] keyStorePassword = PASSWORD;
        try {
            InputStream storeInStream = new FileInputStream(keyStoreFile);
            KeyStore keyStore = KeyStore.getInstance(KeyStore.getDefaultType());
            try {
                keyStore.load(storeInStream, keyStorePassword);
            } catch (IOException ioe) {
                String password = JOptionPane.showInputDialog(null, "KeyStore password: ", "Password", JOptionPane.QUESTION_MESSAGE);
                if (password != null && !password.isEmpty()) {
                    keyStorePassword = password.toCharArray();
                    keyStore.load(storeInStream, keyStorePassword);
                }
            }
            storeInStream.close();

            InputStream certInStream = new FileInputStream(certFile);
            Collection certs = CertificateFactory.getInstance("X.509").generateCertificates(certInStream);
            certInStream.close();

            Iterator certIterator = certs.iterator();
            int i = 0;
            while (certIterator.hasNext()) {
                X509Certificate cert = (X509Certificate) certIterator.next();
                String message = "Add this cert to keystore?\n";
                message += "Issuer: " + cert.getIssuerX500Principal() + "\n";
                message += "Subject: " + cert.getSubjectX500Principal() + "\n";
                message += "Valid from " + cert.getNotBefore() + " to " + cert.getNotAfter() + "\n";
                message += "SerialNumber: " + cert.getSerialNumber() + "\n";
                message += "Signature algorithm name: " + cert.getSigAlgName();
                int result = JOptionPane.showConfirmDialog(null, message, "Add certificate", JOptionPane.YES_NO_CANCEL_OPTION, JOptionPane.QUESTION_MESSAGE);
                if (result == JOptionPane.YES_OPTION) {
                    String alias = certFile.getName() + i++;
                    keyStore.setCertificateEntry(alias, cert);
                } else if (result == JOptionPane.CANCEL_OPTION) {
                    return;
                }
            }

            OutputStream out = new FileOutputStream(keyStoreFile);
            keyStore.store(out, keyStorePassword);
            out.close();

        } catch (Exception ex) {
            JOptionPane.showMessageDialog(null, ex.getMessage(), "Error", JOptionPane.ERROR_MESSAGE);
            logger.log(Level.FINE, null, ex);
        }
    }

    private void showKeyStoreProperties(File keyStoreFile) {
        char[] keyPassword = PASSWORD;
        char[] keyStorePassword = PASSWORD;
        try {
            DefaultTableModel tableModel = (DefaultTableModel) tblServices.getModel();
            tableModel.getDataVector().clear();
            tableModel = (DefaultTableModel) tblProviderProperties.getModel();
            tableModel.getDataVector().clear();
            tableModel = (DefaultTableModel) tblServices.getModel();
            tableModel.getDataVector().clear();
            tableModel = (DefaultTableModel) tblCertificates.getModel();
            tableModel.getDataVector().clear();
            tableModel = (DefaultTableModel) tblKeys.getModel();
            tableModel.getDataVector().clear();
            tableModel = (DefaultTableModel) tblGeneral.getModel();
            tableModel.getDataVector().clear();
            //int index = list.locationToIndex(evt.getPoint());
            if (keyStoreFile.length() == 0) {
                tableModel.addRow(new Object[]{"File name", keyStoreName});
                tableModel.addRow(new Object[]{"File size (bytes)", keyStoreFile.length()});
                tableModel.addRow(new Object[]{"Type", "Empty keystore"});
                return;
            }
            KeyStore keyStore;
            while (true) {
                InputStream in = new FileInputStream(keyStoreFile);
                keyStore = KeyStore.getInstance(KeyStore.getDefaultType());
                try {
                    keyStore.load(in, keyStorePassword);
                    break;
                } catch (IOException ioe) {
                    String password = JOptionPane.showInputDialog(null, "KeyStore password: ", "Password", JOptionPane.QUESTION_MESSAGE);
                    if (password != null && !password.isEmpty()) {
                        keyStorePassword = password.toCharArray();
                    } else {
                        return;
                    }
                } finally {
                    in.close();
                }
            }
            // General tab
            tableModel = (DefaultTableModel) tblGeneral.getModel();
            tableModel.addRow(new Object[]{"File name", keyStoreName});
            tableModel.addRow(new Object[]{"File size (bytes)", keyStoreFile.length()});
            tableModel.addRow(new Object[]{"Type", keyStore.getType()});
            // Provider tab
            txtProviderName.setText(keyStore.getProvider().getName());
            txtProviderVersion.setText(Double.toString(keyStore.getProvider().getVersion()));
            tableModel = (DefaultTableModel) tblProviderProperties.getModel();
            tableModel.addRow(new Object[]{"File name", keyStoreName});
            for (Object key : keyStore.getProvider().keySet()) {
                tableModel.addRow(new Object[]{key.toString(), keyStore.getProvider().get(key)});
            }
            // Services tab
            tableModel = (DefaultTableModel) tblServices.getModel();
            for (Service service : keyStore.getProvider().getServices()) {
                tableModel.addRow(new Object[]{service.getClassName(), service.getAlgorithm(), service.getType()});
            }
            // Certificates + Keys tab
            DefaultTableModel certModel = (DefaultTableModel) tblCertificates.getModel();
            DefaultTableModel keysModel = (DefaultTableModel) tblKeys.getModel();
            Enumeration<String> aliases = keyStore.aliases();
            while (aliases.hasMoreElements()) {
                String alias = aliases.nextElement();
                if (keyStore.isCertificateEntry(alias)) {
                    Certificate certificate = keyStore.getCertificate(alias);
                    certModel.addRow(new Object[]{alias, certificate.getType(), certificate.getEncoded(),
                        certificate.getPublicKey().getAlgorithm(), certificate.getPublicKey().getFormat(), certificate.getPublicKey().getEncoded()});
                }
                if (keyStore.isKeyEntry(alias)) {
                     while (true) {
                        try {
                            Key key = keyStore.getKey(alias, keyPassword);
                            keysModel.addRow(new Object[]{alias, key.getAlgorithm(), key.getFormat(), key.getEncoded()});
                            break;
                        } catch (UnrecoverableKeyException uke) {
                            String password = JOptionPane.showInputDialog(null, "Key password: ", "Password", JOptionPane.QUESTION_MESSAGE);
                            if (password != null && !password.isEmpty()) {
                                keyPassword = password.toCharArray();
                            } else {
                                break;
                            }
                        }
                    }
                }
            }
        } catch (Exception ex) {
            JOptionPane.showMessageDialog(null, ex, "Error", JOptionPane.ERROR_MESSAGE);
            logger.log(Level.FINE, null, ex);
        }
    }

    @Override
    public String getTitle() {
        return "Certificate Manager";
    }

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {
        java.awt.GridBagConstraints gridBagConstraints;

        jDialog1 = new javax.swing.JDialog();
        lblMessage = new javax.swing.JLabel();
        jSplitPane1 = new javax.swing.JSplitPane();
        jPanel1 = new javax.swing.JPanel();
        btnNewKeyStore = new javax.swing.JButton();
        btnDeleteKeyStore = new javax.swing.JButton();
        scrKeyStore = new javax.swing.JScrollPane();
        lstkeyStore = new javax.swing.JList();
        btnImportKeyStore = new javax.swing.JButton();
        btnOpenKeyStore = new javax.swing.JButton();
        tbpKeyStore = new javax.swing.JTabbedPane();
        scrGeneral = new javax.swing.JScrollPane();
        tblGeneral = new javax.swing.JTable();
        pnlCertificates = new javax.swing.JPanel();
        scrCertificates = new javax.swing.JScrollPane();
        tblCertificates = new javax.swing.JTable();
        btnAddCertURL = new javax.swing.JButton();
        btnRemoveCertificate = new javax.swing.JButton();
        btnAddCertFile = new javax.swing.JButton();
        scrKeys = new javax.swing.JScrollPane();
        tblKeys = new javax.swing.JTable();
        scrServices = new javax.swing.JScrollPane();
        tblServices = new javax.swing.JTable();
        pnlProvider = new javax.swing.JPanel();
        lblProvidderName = new javax.swing.JLabel();
        lblProviderVersion = new javax.swing.JLabel();
        scrProviderProperties = new javax.swing.JScrollPane();
        tblProviderProperties = new javax.swing.JTable();
        txtProviderName = new javax.swing.JTextField();
        lblProviderProperties = new javax.swing.JLabel();
        txtProviderVersion = new javax.swing.JTextField();

        setPreferredSize(new java.awt.Dimension(512, 256));
        setLayout(new java.awt.BorderLayout());

        lblMessage.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
        lblMessage.setText("Certificate Manager");
        lblMessage.setVerticalAlignment(javax.swing.SwingConstants.TOP);
        add(lblMessage, java.awt.BorderLayout.CENTER);

        jPanel1.setLayout(new java.awt.GridBagLayout());

        btnNewKeyStore.setText("New keystore");
        btnNewKeyStore.setActionCommand("NEW_KEYSTORE");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.weightx = 0.1;
        gridBagConstraints.insets = new java.awt.Insets(1, 1, 1, 1);
        jPanel1.add(btnNewKeyStore, gridBagConstraints);

        btnDeleteKeyStore.setText("Delete keystore");
        btnDeleteKeyStore.setActionCommand("DELETE_KEYSTORE");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.weightx = 0.1;
        gridBagConstraints.insets = new java.awt.Insets(1, 1, 1, 1);
        jPanel1.add(btnDeleteKeyStore, gridBagConstraints);

        scrKeyStore.setViewportView(lstkeyStore);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.gridwidth = 2;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.weightx = 0.2;
        gridBagConstraints.weighty = 0.1;
        gridBagConstraints.insets = new java.awt.Insets(1, 1, 1, 1);
        jPanel1.add(scrKeyStore, gridBagConstraints);

        btnImportKeyStore.setText("Import");
        btnImportKeyStore.setActionCommand("IMPORT_KEYSTORE");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 2;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.weightx = 0.1;
        gridBagConstraints.insets = new java.awt.Insets(1, 1, 1, 1);
        jPanel1.add(btnImportKeyStore, gridBagConstraints);

        btnOpenKeyStore.setText("Open");
        btnOpenKeyStore.setActionCommand("OPEN_KEYSTORE");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 2;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.weightx = 0.1;
        gridBagConstraints.insets = new java.awt.Insets(1, 1, 1, 1);
        jPanel1.add(btnOpenKeyStore, gridBagConstraints);

        jSplitPane1.setLeftComponent(jPanel1);

        tblGeneral.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "Key", "Value"
            }
        ) {
            Class[] types = new Class [] {
                java.lang.String.class, java.lang.String.class
            };
            boolean[] canEdit = new boolean [] {
                false, false
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        scrGeneral.setViewportView(tblGeneral);

        tbpKeyStore.addTab("General", scrGeneral);

        pnlCertificates.setLayout(new java.awt.GridBagLayout());

        tblCertificates.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "Alias", "Type", "Encoded", "Public key algorithm", "Public key format", "Public key encoded"
            }
        ) {
            Class[] types = new Class [] {
                java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.String.class
            };
            boolean[] canEdit = new boolean [] {
                false, false, false, false, false, false
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        scrCertificates.setViewportView(tblCertificates);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.gridwidth = 2;
        gridBagConstraints.gridheight = 3;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.weightx = 0.1;
        gridBagConstraints.weighty = 0.1;
        gridBagConstraints.insets = new java.awt.Insets(1, 1, 1, 1);
        pnlCertificates.add(scrCertificates, gridBagConstraints);

        btnAddCertURL.setText("Add certificate from URL");
        btnAddCertURL.setActionCommand("ADD_CERT_URL");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 2;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.insets = new java.awt.Insets(1, 1, 1, 1);
        pnlCertificates.add(btnAddCertURL, gridBagConstraints);

        btnRemoveCertificate.setText("Remove certificate");
        btnRemoveCertificate.setActionCommand("REMOVE_CERT");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 2;
        gridBagConstraints.gridy = 2;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.PAGE_START;
        gridBagConstraints.insets = new java.awt.Insets(1, 1, 1, 1);
        pnlCertificates.add(btnRemoveCertificate, gridBagConstraints);

        btnAddCertFile.setText("Add certificate from file");
        btnAddCertFile.setActionCommand("ADD_CERT_FILE");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 2;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.PAGE_START;
        gridBagConstraints.insets = new java.awt.Insets(1, 1, 1, 1);
        pnlCertificates.add(btnAddCertFile, gridBagConstraints);

        tbpKeyStore.addTab("Certificates", pnlCertificates);

        tblKeys.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "Class name", "Algorithm", "Type"
            }
        ) {
            Class[] types = new Class [] {
                java.lang.String.class, java.lang.String.class, java.lang.String.class
            };
            boolean[] canEdit = new boolean [] {
                false, false, false
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        scrKeys.setViewportView(tblKeys);

        tbpKeyStore.addTab("Keys", scrKeys);

        tblServices.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "Class name", "Algorithm", "Type"
            }
        ) {
            Class[] types = new Class [] {
                java.lang.String.class, java.lang.String.class, java.lang.String.class
            };
            boolean[] canEdit = new boolean [] {
                false, false, false
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        scrServices.setViewportView(tblServices);

        tbpKeyStore.addTab("Services", scrServices);

        pnlProvider.setLayout(new java.awt.GridBagLayout());

        lblProvidderName.setText("Name");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.insets = new java.awt.Insets(1, 1, 1, 1);
        pnlProvider.add(lblProvidderName, gridBagConstraints);

        lblProviderVersion.setText("Version");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.insets = new java.awt.Insets(1, 1, 1, 1);
        pnlProvider.add(lblProviderVersion, gridBagConstraints);

        tblProviderProperties.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "Key", "Value"
            }
        ) {
            Class[] types = new Class [] {
                java.lang.String.class, java.lang.String.class
            };
            boolean[] canEdit = new boolean [] {
                false, false
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        scrProviderProperties.setViewportView(tblProviderProperties);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 3;
        gridBagConstraints.gridwidth = 2;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.weightx = 0.1;
        gridBagConstraints.weighty = 0.1;
        gridBagConstraints.insets = new java.awt.Insets(1, 1, 1, 1);
        pnlProvider.add(scrProviderProperties, gridBagConstraints);

        txtProviderName.setEditable(false);
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.weightx = 0.1;
        gridBagConstraints.insets = new java.awt.Insets(1, 1, 1, 1);
        pnlProvider.add(txtProviderName, gridBagConstraints);

        lblProviderProperties.setText("Properties");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 2;
        gridBagConstraints.gridwidth = 2;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.weightx = 0.1;
        gridBagConstraints.insets = new java.awt.Insets(1, 1, 1, 1);
        pnlProvider.add(lblProviderProperties, gridBagConstraints);

        txtProviderVersion.setEditable(false);
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.weightx = 0.1;
        gridBagConstraints.insets = new java.awt.Insets(1, 1, 1, 1);
        pnlProvider.add(txtProviderVersion, gridBagConstraints);

        tbpKeyStore.addTab("Provider", pnlProvider);

        jSplitPane1.setRightComponent(tbpKeyStore);

        add(jSplitPane1, java.awt.BorderLayout.CENTER);
    }// </editor-fold>//GEN-END:initComponents
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnAddCertFile;
    private javax.swing.JButton btnAddCertURL;
    private javax.swing.JButton btnDeleteKeyStore;
    private javax.swing.JButton btnImportKeyStore;
    private javax.swing.JButton btnNewKeyStore;
    private javax.swing.JButton btnOpenKeyStore;
    private javax.swing.JButton btnRemoveCertificate;
    private javax.swing.JDialog jDialog1;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JSplitPane jSplitPane1;
    private javax.swing.JLabel lblMessage;
    private javax.swing.JLabel lblProvidderName;
    private javax.swing.JLabel lblProviderProperties;
    private javax.swing.JLabel lblProviderVersion;
    private javax.swing.JList lstkeyStore;
    private javax.swing.JPanel pnlCertificates;
    private javax.swing.JPanel pnlProvider;
    private javax.swing.JScrollPane scrCertificates;
    private javax.swing.JScrollPane scrGeneral;
    private javax.swing.JScrollPane scrKeyStore;
    private javax.swing.JScrollPane scrKeys;
    private javax.swing.JScrollPane scrProviderProperties;
    private javax.swing.JScrollPane scrServices;
    private javax.swing.JTable tblCertificates;
    private javax.swing.JTable tblGeneral;
    private javax.swing.JTable tblKeys;
    private javax.swing.JTable tblProviderProperties;
    private javax.swing.JTable tblServices;
    private javax.swing.JTabbedPane tbpKeyStore;
    private javax.swing.JTextField txtProviderName;
    private javax.swing.JTextField txtProviderVersion;
    // End of variables declaration//GEN-END:variables

    @Override
    public JComponent getComponent() {
        return this;
    }

    @Override
    public Icon getIcon() {
        return null;
    }

    @Override
    public CacheMode getCacheMode() {
        return CacheMode.SUPPORTS;
    }
}
